package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.common.dto.DocDto;
import com.koron.css2.common.service.AbstractExportDoc;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.query.PjInfoQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Tools;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 施工勘察节点：用户报装工作流程表打印
 */
@Slf4j
@Service
public class InstallationProcessTable extends AbstractExportDoc {
    private static final String PJ_TYPE = "PPT";
    private static final String CERT_TYPE = "IDT";
    @Override
    public DocDto getDocText(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try{
            Map<String,Object> param=new HashMap<>();
            //获取参数
            PjInfoQueryBean pjInfoQueryBean = JsonUtils.objectToPojo(req.getData(), PjInfoQueryBean.class);
            //获取工程信息
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            PjInfoBean pjInfoBean = pjInfoMapper.getSingle(pjInfoQueryBean);
            //获取客户信息
            CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
            CtmInfoBean ctmInfoBean = ctmMapper.getCtmInfoById(pjInfoBean.getUserCtmTempId(), 2);
            //  获取工程类型数据字典
            Map<String, String> pjTypeMap = Tools.mapDicByCode(factory, PJ_TYPE);
            //获取身份证类型数据字典
            Map<String, String> certTypeMap = Tools.mapDicByCode(factory, CERT_TYPE);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(pjInfoBean.getBillDate());
            param.put("billDate",dateString);
            param.put("billNo",pjInfoBean.getBillNo());
            param.put("ctmName",ctmInfoBean.getCtmName());
            param.put("ctmAddr",ctmInfoBean.getCtmAddr());
            param.put("mobile",ctmInfoBean.getMobile());
            param.put("linkTel",ctmInfoBean.getLinkTel());
            if(pjInfoBean!=null&&pjTypeMap!=null){
                if(pjInfoBean.getPjType()!=null&&pjTypeMap.containsKey(pjInfoBean.getPjType())){
                    param.put("pjType",pjTypeMap.get(pjInfoBean.getPjType()));
                }
            }
            if(ctmInfoBean!=null&&certTypeMap!=null){
                if(ctmInfoBean.getCertType()!=null&&certTypeMap.containsKey(ctmInfoBean.getCertType())){
                    param.put("certType",certTypeMap.get(ctmInfoBean.getCertType()));
                }
            }
            param.put("certNo",ctmInfoBean.getCertNo());
            DocDto docDto = new DocDto();
            docDto.setTitle("用水报装工作流程表");
            docDto.setTemplate("用水报装工作流程表.docx");
            docDto.setParam(param);
            return docDto;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new RuntimeException("获取用水报装工作流程表内容失败。");
        }
    }
}
